classdef TSP<handle
    properties
        city_data = [];
        city_num = 0;
        distence = [];
        path = [];
        length = 0;
    end
    methods
        function obj=TSP(city_data)
           obj.city_data = city_data;
           obj.city_num = max([length(city_data(:,1));length(city_data(1,:))]);
           obj.distence = 1e-8*ones(obj.city_num,obj.city_num);
           obj.path = 1:obj.city_num;
           for i = 1:obj.city_num
               for j = i+1:obj.city_num
                   obj.distence(i,j) = sqrt(sum(obj.city_data(i,:)-obj.city_data(j,:)).^2);
                   obj.distence(j,i) = obj.distence(i,j); 
               end
           end
        end
        function update_path(obj,path)
            obj.path = path;
        end
        function draw_path(obj)
            scatter(obj.city_data(:,2),obj.city_data(:,1),'r*');hold on;
            x = [obj.city_data(obj.path,2);obj.city_data(obj.path(1),2)];
            y = [obj.city_data(obj.path,1);obj.city_data(obj.path(1),1)];
            plot(x,y,'LineWidth',1.2,'Color',[0 0.4470 0.7410]);
            text(x(1),y(1)+2,'起点','FontSize',14,'Color',[0.8500 0.3250 0.0980]);
        end
        function len = path_length(obj)
            obj.length = 0;
            for i = 2:obj.city_num
                obj.length = obj.length + obj.distence(obj.path(i-1),obj.path(i)); 
            end
            obj.length = obj.length + obj.distence(obj.path(obj.city_num),obj.path(1)); 
            len = obj.length;
        end
        function str = print_path(obj)
            str1 = num2str(obj.path(1));
            str2 = '        ';
            for i = 2:obj.city_num
                if i <= 20
                    str1 = [str1,'->',num2str(obj.path(i))];
                else
                    str2 = [str2,'->',num2str(obj.path(i))];
                end    
            end
            str2 = [str2,'->',num2str(obj.path(1))];
            disp(str1)
            disp(str2)
            str = [str1,str2];
        end
    end
    methods(Static,Access = 'private')
        
    end 
end